package Service.Impl;

import Dao.UserDao;

import Model.User;
import Model.UserQuery;
import Service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import utils.ReturnData;
import utils.ReturnDataForLayui;
import utils.UpdateOrInsertResultDeal;
import utils.pages.Page;
import utils.pages.PageBean;
import utils.permission.PageDataResult;

import javax.servlet.http.HttpSession;

@Service("userService")
public class UserServicelmpl implements UserService {

    @Autowired
    private UserDao userDao;

    /**
     * 新增派工订单-默认机构信息显示
     * @return
     */
    @Override
    public String findSettleCodebyUserNumber(String UserNumber){
        return userDao.findSettleCodebyUserNumber(UserNumber);
    }

    /**
     * 护工页面-机构权限
     * @return
     */
    @Override
    public String findRoleIDbyUserNumber(String UserNumber){
        return userDao.findRoleIDbyUserNumber(UserNumber);
    }


    /**
     * 用户登陆：通过user_number查找
     * @param
     * @param query
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    @Override
    public PageBean<User> getList(UserQuery query) {
//        PageHelper.startPage(user.getPage(), user.getLimit());
        int count = query.getCount() > 0? query.getCount(): userDao.countList(query);
        if(count > 0){
            PageBean<User> pageBean = new PageBean<>();
//            query.setPagination(true);
            pageBean.setData(userDao.getList(query));
            pageBean.setCount(count);
            return pageBean;
        }

        return null;
    }
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    @Override
    public ReturnDataForLayui getName(User user) {
        PageHelper.startPage(user.getPage(), user.getLimit());
        List<User> list = userDao.getName(user);
        PageInfo<User> info = new PageInfo<>(list);
        return ReturnDataForLayui.success(list, info.getTotal());
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public ReturnData add(User user) {
        int i = userDao.add(user);
        return UpdateOrInsertResultDeal.dealWith(i);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public ReturnData delete(User user) {
        int i = userDao.delete(user);
        return UpdateOrInsertResultDeal.dealWith(i);
    }


    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public boolean update(User user) {
        int i = userDao.update(user);
        return i > 0;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public ReturnData updateState(User user) {
        int i = userDao.updateState(user);
        return UpdateOrInsertResultDeal.dealWith(i);
    }

    @Override
    public String setuser(User user, HttpSession session) {
//        if (user.getId()!= null) {
//            //!=null说明是编辑
//            Date date = new Date();
//            Timestamp timestamp = new Timestamp(date.getTime());
//            user.setUpdateTime(timestamp);
//            this.userDao.updateuserById(user);
//        } else {
//            // 新增用户
//            Date date = new Date();
//            Timestamp timestamp = new Timestamp(date.getTime());
//            user.setCreateTime(timestamp);
//            user.setUpdateTime(timestamp);
//            this.userDao.insertuser(user);
//        }
        return "ok";
    }

    @Override
    public User getuser(Integer id) {
        return userDao.getuser(id);
    }


    @Override
    public PageDataResult getusers(User userSearch, int page, int limit) {
        System.out.println("page:" + page + "limit:" + limit);

        PageDataResult pdr = new PageDataResult();
        System.out.println(pdr.toString());
        PageHelper.startPage(page, limit);
        List<User> devList = userDao.getusers(userSearch);
        // 获取分页查询后的数据
        PageInfo<User> pageInfo = new PageInfo<>(devList);
        // 设置获取到的总记录数total：
        pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
        pdr.setList(devList);
        System.out.println(pdr.toString());
        return pdr;
    }

    @Override
    public String deluser(Integer id) {
        return this.userDao.deluser(id) == 1 ? "ok"
                : "删除失败，请您稍后再试";
    }

    /**
     * 用户登陆：通过user_number查找
     * @param user_number
     * @return
     */
    @Override
    public User findUserByName(String user_number) {
        System.out.println("service:用户查找");
        User user=userDao.findUserByName(user_number);
        System.out.println("service:用户查找完毕");
        System.out.println("service:用户查找"+user);
        return userDao.findUserByName(user_number);
    }

    /**
     * 用户登陆：通过user_number和password查找
     *
     */
    @Override
    public User findUser(User user){
        return userDao.findUser(user.getUser_number(),user.getPassword());
    }

    /**
     * 通过id查询用户
     */
    @Override
    public User getUserById(Integer id) {
        User user = userDao.getUserById(id);
        return user;
    }

    @Override
    public User getUserByName(String name) {
        User user = userDao.getUserByName(name);
        return user;
    }

    @Override
    public User getUserBySex(String sex) {
        User user = userDao.getUserBySex(sex);
        return user;
    }

    /**
     * 更新用户
     */
    @Override
    public int updateUser(User user) {
        return userDao.updateUser(user);
    }

    @Override
    public Page<User> Show_All_User() {
        return userDao.Show_All_User();
    }


    @Override
    public List<User> getUserId(int user_id) {
        return userDao.selectByUserId(user_id);
    }

    @Override
    public List<User> getUserName(String user_name) {
        return userDao.selectByUserName(user_name);
    }

    @Override
    public List<User> getUserSex(String user_sex) {
        return userDao.selectByUserSex(user_sex);
    }

}
